package com.winchain.provider;

import com.shell.bee.base.exception.UnCheckException;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.persistence.transaction.Transaction;
import com.shell.bee.persistence.transaction.TransactionUtil;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.basic.exception.ExtException;
import com.winchain.chainbill.bill.base.po.CbInsAcr;
import com.winchain.chainbill.bill.base.service.CbInsAcrService;
import com.winchain.chainbill.bill.biz.receive.vo.CbDfrlVO;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DelayCalculatedInterestExtProvider {
	private static final Log logger = LogFactory.getLog(DelayCalculatedInterestExtProvider.class);
	@Autowired
	private CbInsAcrService cbInsAcrService;

	@RequestMapping({ "/delayInteres/batchJob" })
	public void delayCalculatedInterestBatchJob(@RequestParam("workDate") String workDate) {
		logger.info("--------delayCalculatedInterestBatchJob excute start--------");
		try {
			List<CbDfrlVO> cbDfrlVOList = this.cbInsAcrService.selectCbInsAcrDfrlInfos();
			for (final CbDfrlVO vo : cbDfrlVOList) {
				if (!vo.getEndDate().equals(vo.getExpDate())) {
					TransactionUtil.execute(new Transaction() {
						public void doInTransaction() {
							CbInsAcr cbInsAcr = new CbInsAcr();
							cbInsAcr.setAcrId(SystemUtil.getUUID());
							cbInsAcr.setBizId(vo.getBizId());
							String endDate = vo.getEndDate();
							String preExpDate = null;
							try {
								preExpDate = SysDateUtil.getPreDay(endDate, 1);
							} catch (ParseException e) {
								e.printStackTrace();
								throw new UnCheckException();
							}
							cbInsAcr.setStartDate(preExpDate);
							String expDateStr = vo.getExpDate();
							String expDateSubStr = expDateStr.substring(0, 6);
							String endDateStr = SysDateUtil.nextOneDay(preExpDate, 20, "yyyyMMdd");
							String endDateSubStr = endDateStr.substring(0, 6);
							if (SysDateUtil.compare(endDateSubStr, expDateSubStr) >= 0) {
								endDateStr = expDateStr;
							}
							cbInsAcr.setEndDate(endDateStr);
							cbInsAcr.setAcrMonth(endDateStr.substring(0, 6));
							cbInsAcr.setAcrInsType("2");
							int days = SysDateUtil.getDaysBetween(preExpDate, endDateStr, "yyyyMMdd").intValue() + 1;
							BigDecimal dfrRate = vo.getDfrRate();
							BigDecimal billAmt = vo.getBillAmt();
							BigDecimal curIns = billAmt.multiply(new BigDecimal(days))
									.multiply(dfrRate.multiply(new BigDecimal("0.01")))
									.divide(new BigDecimal(360), 2, 4);
							if (expDateSubStr.equals(endDateSubStr)) {
								curIns = vo.getDfrInt().subtract(vo.getTotIns());
							}
							cbInsAcr.setCurIns(curIns);
							cbInsAcr.setEarlyIns(vo.getTotIns());
							cbInsAcr.setTotIns(vo.getTotIns().add(curIns));
							cbInsAcr.setCreateTime(SysDateUtil.getSystemDateTime());
							DelayCalculatedInterestExtProvider.this.cbInsAcrService.save(cbInsAcr);
						}
					}, new int[0]);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("������������������������", e);
			throw new ExtException(e.getMessage());
		}
		logger.info("--------delayCalculatedInterestBatchJob excute end--------");
	}
}
